﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace lab1
{
    class BookStore:List<Book>
    {
        public void Input()
        {
            String id, name, publisher, price;
            bool tmp1 = true;
            do
            {
                Console.WriteLine("Enter ID");
                id = Console.ReadLine();
                if (string.IsNullOrEmpty(id) == true) tmp1 = false;
                else
                    tmp1 = true;
                foreach (Book c in this)
                {
                    if (c.Id.Equals(id)) tmp1 = false;
                    else tmp1 = true;
                }

            } while (tmp1 == false);

            do
            {
                Console.WriteLine("Enter Book name ");
                name = Console.ReadLine();
                if (string.IsNullOrEmpty(name) == false) break;

            } while (true);
            do
            {
                Console.WriteLine("Enter Book Publisher: ");
                publisher = Console.ReadLine();
                if (string.IsNullOrEmpty(publisher) == false) break;
            } while (true);
            do
            {
                Console.WriteLine("Enter Book price ");
                price = Console.ReadLine();
                if (string.IsNullOrEmpty(price) == false) break;
            } while (true);
            Book tmp = new Book(id, name, publisher, price);
            this.Add(tmp);
        }
        public bool valid(Book book) 
        {
            foreach (Book tmp in this)
            {
                if (tmp.Id.Equals(book.Id))
                {
                    Console.WriteLine("Book ID {0} is existed", book.Id);
                    return false;
                }
            }
            try
            {
                int tmp = int.Parse(book.Price);
                

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Price must be number");
                return false;
            }
            return true;
            
        }
        public void remove()
        {
            Console.WriteLine("Enter ID");
            string id = Console.ReadLine();
            foreach (Book tmp in this)
            {
                if (tmp.Id.Equals(id))
                {
                    this.Remove(tmp);
                }
            }
        }
        public void searchbyName()
        {
            string name;
            BookStore searchResult=new BookStore();
            Console.WriteLine("Enter Name of Book you want to search");
            name = Console.ReadLine();
            foreach(Book tmp in this){
                if (tmp.Name.Equals(name))
                    searchResult.Add(tmp);
            }
            if (!this.Any<Book>())
            {
                Console.WriteLine("Don't exist Book has name is {0}",name);
            }
            else
            {
                Console.WriteLine("Result");
                Console.WriteLine(searchResult);
            }
        }
        public void Update() {
            String id,name,publisher,price;
            Console.WriteLine("Enter ID:");
            id = Console.ReadLine();
            foreach (Book tmp in this)
            {
                if (tmp.Id.Equals(id))
                {
                    Console.WriteLine("Enter Name:");
                    name = Console.ReadLine();
                    if (string.IsNullOrEmpty(name) != true)
                    {
                        tmp.Name = name;
                    }
                    Console.WriteLine("Enter Publisher:");
                    publisher = Console.ReadLine();

                    if (string.IsNullOrEmpty(publisher) != true)
                    {
                        tmp.Publisher = publisher;
                    }
                    Console.WriteLine("Enter price:");
                    price = Console.ReadLine();

                    if (string.IsNullOrEmpty(price) != true)
                    {
                        tmp.Price = price;
                    }
                }
                else
                {
                    Console.WriteLine("You ID you input is not exist");
                }

            }
            
        }
        public void sortbyprice() {
            Book tmp;
            for (int i = 0; i < this.Count(); i++)
                for (int j = 0; j < this.Count(); j++)
            {
                if (this[i].Price.CompareTo(this[j].Price)>0)
                {
                    tmp = this[i];
                    this[i] = this[j];
                    this[j] = tmp;
                }
            }
        
        }
        public void Display()
        {
            try
            {
                Console.WriteLine("1.Sort By Name");
                Console.WriteLine("2.Sort By price");
                Console.WriteLine("Choose:");
                int choose = Convert.ToInt32(Console.ReadLine());
                switch (choose)
                {
                    case 1: this.Sort();
                        Console.WriteLine(this.ToString());
                        break;
                    case 2: this.sortbyprice();
                        Console.WriteLine(this);
                        break;
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
        }
        public override string ToString()
        {
            String s = "";
            Console.WriteLine("*********************** Book Store ***********************");
            Console.WriteLine("{0,-10}{1,-20}{2,-20}{3,-10}","ID","Name","Publisher","Price");
            foreach (Book tmp in this)
            {
                s = s + string.Format("{0,-10}{1,-20}{2,-20}{3,-10}\n", tmp.Id, tmp.Name, tmp.Publisher, tmp.Price);
                
            }
            return s;
        }
    }
}
